package com.chilly.other.jdk8.Interface;

import org.junit.Test;

import java.lang.reflect.Method;

/**
 * 通过对象名引用成员方法
 *
 * Created by Chilly Cui on 2021/1/6.
 */
public class RefMethodByObjTest {
    public void printResult(String s, Printable printable) {
        printable.print(s);
    }

    //传统方法
    @Test
    public void testPrintResult() {
        printResult("hello lambda", new Printable() {
            @Override
            public void print(String s) {
                Method obj = new Method(); //创建Method对象
                obj.printUpperCaseStr(s); //调用对象的成员方法,将字符串转换为大写后输出
            }
        });
    }

    //Lambda表达式写法
    @Test
    public void testPrintResultWithLambda() {
        printResult("hello lambda", s -> {
            Method obj = new Method();
            obj.printUpperCaseStr(s);
        });
    }

    //方法引用写法
    @Test
    public void testPrintResultWithMethodRef() {
        Method obj = new Method();
        printResult("hello lambda", obj::printUpperCaseStr);
    }

    //定义含有成员方法类
    class Method {
        public void printUpperCaseStr(String s) {
            System.out.println(s.toUpperCase());
        }
    }
}

